//
//  TiDanListCell.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/9/11.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "TiDanListCell.h"

@interface TiDanListCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *diaoDuTimeLab;
@property (nonatomic, strong) UILabel *timeLimitLab;
@property (nonatomic, strong) UILabel *diaoDuCarLab;
@property (nonatomic, strong) UIView *priceBGView;
@property (nonatomic, strong) UIButton *jieDanBtn;
@property (nonatomic, strong) UILabel *priceTitleLab;
@property (nonatomic, strong) UILabel *priceLab;

@end

@implementation TiDanListCell

+ (instancetype)TiDanListCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"TiDanListCell";
    TiDanListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[TiDanListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.bgView = [UIView new];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.nameLab = [UILabel new];
        self.nameLab.textColor = HexColor(0x2B2C44);
        self.nameLab.font = BoldFontOfSize(18);
        [self.bgView addSubview:self.nameLab];
        
        self.timeLimitLab = [UILabel new];
        self.timeLimitLab.backgroundColor = HexColorA(0xF35959, 0.1);
        self.timeLimitLab.textColor = HexColor(0xF35959);
        self.timeLimitLab.layer.masksToBounds = YES;
        self.timeLimitLab.layer.cornerRadius = 4;
        self.timeLimitLab.font = BoldFontOfSize(20);
        self.timeLimitLab.textAlignment = NSTextAlignmentCenter;
        [self.bgView addSubview:self.timeLimitLab];
        
        self.diaoDuTimeLab = [UILabel new];
        self.diaoDuTimeLab.textColor = HexColor(0x2B2C44);
        self.diaoDuTimeLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.diaoDuTimeLab];
        
        self.diaoDuCarLab = [UILabel new];
        self.diaoDuCarLab.textColor = HexColor(0x2B2C44);
        self.diaoDuCarLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.diaoDuCarLab];
        
        self.priceBGView = [UIView new];
        self.priceBGView.backgroundColor = HexColor(0xF7F7FA);
        [self.bgView addSubview:self.priceBGView];
        
        self.priceTitleLab = [UILabel new];
        self.priceTitleLab.textColor = HexColor(0x9C9DB3);
        self.priceTitleLab.font = [UIFont systemFontOfSize:14];
        [self.priceBGView addSubview:self.priceTitleLab];
        
        self.priceLab = [UILabel new];
        self.priceLab.textColor = HexColor(0x2B2C44);
        self.priceLab.font = BoldFontOfSize(20);
        [self.priceBGView addSubview:self.priceLab];
        
        self.jieDanBtn = [UIButton new];
        self.jieDanBtn.backgroundColor = Color_ThemeColor;
        [self.jieDanBtn setTitle:@"接单" forState:0];
        [self.jieDanBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.jieDanBtn.titleLabel.font = BoldFontOfSize(14);
        [self.jieDanBtn addTarget:self action:@selector(jieDanClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.jieDanBtn];
    }
    return self;
}

- (void)configWithTime:(long)downtime {
    if (downtime > 0) {
        NSInteger days = (int)(downtime/(3600*24));
        NSInteger hours = (int)((downtime-days*24*3600)/3600);
        NSInteger minute = (int)(downtime-days*24*3600-hours*3600)/60;
        NSInteger second = downtime - days*24*3600 - hours*3600 - minute*60;
        self.timeLimitLab.text = [NSString stringWithFormat:@"%02ld:%02ld",(long)minute,(long)second];
    }else {
        self.timeLimitLab.text = @"00:00";
        if (self.refreshBlock) {
            self.refreshBlock();
        }
    }
}

- (void)jieDanClick {
    if (self.jieDanBlock) {
        self.jieDanBlock();
    }
}

-(void)setModel:(TiDanMainModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.bgView.nim_left = 15;
    self.bgView.nim_top = 10;
    self.bgView.nim_width = ScreenW - 30;
    
    self.nameLab.text = self.model.tlogistics_name;
    self.nameLab.nim_left = 10;
    self.nameLab.nim_top = 15;
    [self.nameLab sizeToFit];
    self.nameLab.nim_width = self.bgView.nim_width - 100 - 10;
    
    self.diaoDuTimeLab.text = [NSString stringWithFormat:@"调度时间：%@",self.model.createTime];
    self.diaoDuTimeLab.nim_left = self.nameLab.nim_left;
    self.diaoDuTimeLab.nim_top = self.nameLab.nim_bottom + 5;
    [self.diaoDuTimeLab sizeToFit];
    self.diaoDuTimeLab.nim_width = self.bgView.nim_width - 20;
    
    CGFloat bottom_max = self.diaoDuTimeLab.nim_bottom;
    if (self.model.vehicle_num.length) {
        self.diaoDuCarLab.text = [NSString stringWithFormat:@"调度车辆：%@",self.model.vehicle_num];
        self.diaoDuCarLab.nim_left = self.nameLab.nim_left;
        self.diaoDuCarLab.nim_top = self.diaoDuTimeLab.nim_bottom + 5;
        [self.diaoDuCarLab sizeToFit];
        self.diaoDuCarLab.nim_width = self.bgView.nim_width - 20;
        bottom_max = self.diaoDuCarLab.nim_bottom;
    }else {
        self.diaoDuCarLab.frame = CGRectZero;
    }
    
    if ([self.model.is_accept intValue] == 2) {
        self.priceBGView.nim_left = self.nameLab.nim_left;
        self.priceBGView.nim_top = bottom_max + 10;
        self.priceBGView.nim_width = self.bgView.nim_width - 20 - 70;
        self.priceBGView.nim_height = 36;
        [self.priceBGView setBorderWithCornerRadius:6 borderWidth:0 borderColor:[UIColor clearColor] type:UIRectCornerTopLeft | UIRectCornerBottomLeft];
        
        self.jieDanBtn.nim_left = self.priceBGView.nim_right;
        self.jieDanBtn.nim_top = self.priceBGView.nim_top;
        self.jieDanBtn.nim_width = 70;
        self.jieDanBtn.nim_height = 36;
        [self.jieDanBtn setBorderWithCornerRadius:6 borderWidth:0 borderColor:[UIColor clearColor] type:UIRectCornerTopRight | UIRectCornerBottomRight];
        
        self.priceTitleLab.text = @"参考价格";
        [self.priceTitleLab sizeToFit];
        self.priceTitleLab.nim_left = 7;
        self.priceTitleLab.nim_top = self.priceBGView.nim_height - 5 - self.priceTitleLab.nim_height;
        
        self.priceLab.text = [NSString stringWithFormat:@"¥%@",self.model.price];
        [self.priceLab sizeToFit];
        self.priceLab.nim_left = self.priceTitleLab.nim_right + 3;
        self.priceLab.nim_bottom = self.priceTitleLab.nim_bottom + 2;
        
        self.bgView.nim_height = self.priceBGView.nim_bottom + 10;
        
        self.timeLimitLab.nim_left = self.bgView.nim_width - 80;
        self.timeLimitLab.nim_top = 1;
        self.timeLimitLab.nim_width = 79;
        self.timeLimitLab.nim_height = 30;
//        [self timeLimit];
        if (self.model.downtime > 0) {
            NSInteger days = (int)(self.model.downtime/(3600*24));
            NSInteger hours = (int)((self.model.downtime-days*24*3600)/3600);
            NSInteger minute = (int)(self.model.downtime-days*24*3600-hours*3600)/60;
            NSInteger second = self.model.downtime - days*24*3600 - hours*3600 - minute*60;
            self.timeLimitLab.text = [NSString stringWithFormat:@"%02ld:%02ld",(long)minute,(long)second];
        }else {
            self.timeLimitLab.text = @"00:00";
        }
    }else {
        self.bgView.nim_height = bottom_max + 15;
        self.timeLimitLab.frame = CGRectZero;
        self.priceBGView.frame = CGRectZero;
        self.priceTitleLab.frame = CGRectZero;
        self.priceLab.frame = CGRectZero;
        self.jieDanBtn.frame = CGRectZero;
    }
    
    self.height = self.bgView.nim_bottom;
}

- (void)timeLimit {
    __block long timeout = self.model.downtime; //倒计时时间
    if (timeout > 0) {
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0*NSEC_PER_SEC,  0*NSEC_PER_SEC); //每秒执行
        dispatch_source_set_event_handler(timer, ^{
            if(timeout <= 0){ //  当倒计时结束时做需要的操作: 关闭 活动到期不能提交
                dispatch_source_cancel(timer);
//                timer = nil;
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.timeLimitLab.text = @"00:00";
                });
            } else { // 倒计时重新计算 时/分/秒
                NSInteger days = (int)(timeout/(3600*24));
                NSInteger hours = (int)((timeout-days*24*3600)/3600);
                NSInteger minute = (int)(timeout-days*24*3600-hours*3600)/60;
                NSInteger second = timeout - days*24*3600 - hours*3600 - minute*60;
                dispatch_async(dispatch_get_main_queue(), ^{
                    self.timeLimitLab.text = [NSString stringWithFormat:@"%02ld:%02ld",(long)minute,(long)second];
                });
                timeout--; // 递减 倒计时-1(总时间以秒来计算)
            }
        });
        dispatch_resume(timer);
    }else {
        self.timeLimitLab.text = @"00:00";
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//-(void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}

@end
